package com.yra.controller.manager;

import com.yra.domain.Carousel;
import com.yra.service.CarouselService;
import com.yra.util.PageQueryUtil;
import com.yra.util.Result;
import com.yra.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: yrq
 * @Date: Create in 9:21 2019/12/17
 */
@Controller
@RequestMapping("manager")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;

    @RequestMapping(value = "/carousel")
    public ModelAndView goToCarousel(HttpServletRequest request){
        request.setAttribute("path", "newbee_mall_carousel");
        return new ModelAndView("/admin/newbee_mall_carousel");
    }

    @RequestMapping(value = "/carousel/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result> list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult("参数异常！"));
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        return ResponseEntity.status(HttpStatus.OK).body(ResultGenerator.genSuccessResult(carouselService.getCarouselPage(pageQueryUtil)));
    }

    @RequestMapping(value = "/carousels/save",method = RequestMethod.POST)
    public ResponseEntity<String> saveCarousel(@RequestBody Carousel carousel){
        carouselService.insertCarousel(carousel);
        return ResponseEntity.status(HttpStatus.OK).body("insert");
    }

    @RequestMapping(value = "/carousels/update",method = RequestMethod.POST)
    public ResponseEntity<String> updateCarousel(@RequestBody Carousel carousel){
        carouselService.updateCarousel(carousel);
        return ResponseEntity.status(HttpStatus.OK).body("update");
    }

    @RequestMapping(value = "/carousels/delete",method = RequestMethod.POST)
    public ResponseEntity<Boolean> deleteCarousel(@RequestBody int[] ids){
        carouselService.deleteCarouselByIds(ids);
        return ResponseEntity.status(HttpStatus.OK).body(true);
    }
}
